<div class="page-header">
    <h1>
        Form Tambah Harga Produk
        <small>
            <i class="icon-double-angle-right"></i>
            Penambahan Harga Produk
        </small>
    </h1>
</div>

<?php
if ($this->errmsg) {
    ?>
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-remove"></i>
            Error
        </strong>

        <?php echo $this->errmsg; ?>
        <br />
    </div>
    <?php
}

if ($this->msg) {
    ?>
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-ok"></i>
        </strong>

        <?php echo $this->msg; ?>
        <br />
    </div>
    <?php
}

if (!$this->msg && !$this->errmsg) {
    ?>

    <div class="row">
        <div class="col-xs-11">
            <form id="formAdd" class="form-horizontal" role="form" action="" method="post">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="suplier_id"> Parent Produk </label>

                        <div class="col-sm-9">
                            <input type="text" id="parent_product_name" name="parent_product_name" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['parent_product_name'] )) { echo $this->data['parent_product_name'];} ?>" readonly />
                            <input type="hidden" id="parent_product_id" name="parent_product_id" value="<?php if (isset( $this->data['parent_product_id'] )) { echo $this->data['parent_product_id'];} ?>" />

			</div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="code"> Kode Produk </label>

                    <div class="col-sm-9">
                        <input type="text" id="product_code" name="product_code" placeholder="Kode Produk" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['product_code'] )) { echo $this->data['product_code'];} ?>" readonly />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="product_name"> Nama Produk </label>

                    <div class="col-sm-9">
                        <input type="text" id="product_name" name="product_name" placeholder="Nama Produk" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['product_name'] )) { echo $this->data['product_name'];} ?>" readonly />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="product_desc"> Deskripsi Produk </label>

                    <div class="col-sm-9">
                        <input type="text" id="product_desc" name="product_desc" placeholder="Deskripsi Produk" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['product_desc'] )) { echo $this->data['product_desc'];} ?>" readonly />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="product_owner_id"> Owner </label>

                        <div class="col-sm-9">
                            <input type="text" id="product_owner_name" name="product_owner_name" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['product_owner_name'] )) { echo $this->data['product_owner_name'];} ?>" readonly />
                            <input type="hidden" id="product_owner_id" name="product_owner_id" value="<?php if (isset( $this->data['product_owner_id'] )) { echo $this->data['product_owner_id'];} ?>" />

			</div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="suplier_id"> Suplier </label>

                        <div class="col-sm-9">
                            <input type="text" id="suplier_name" name="suplier_name" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['suplier_name'] )) { echo $this->data['suplier_name'].' - '.$this->data['suplier_address'];} ?>" readonly />
                            <input type="hidden" id="suplier_id" name="suplier_id" value="<?php if (isset( $this->data['suplier_id'] )) { echo $this->data['suplier_id'];} ?>" />
			</div>

                </div>
                <hr>

                
                <div class="clearfix form-actions">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="basic_price"> Harga Asal </label>

                    <div class="col-sm-9">
                        <input type="text" id="basic_price" name="basic_price" placeholder="Tankpa titik/koma (cth: 150000)" class="col-xs-10 col-sm-5" onkeypress='return validateNumber(event)' required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="sale_price"> Harga Jual </label>

                    <div class="col-sm-9">
                        <input type="text" id="sale_price" name="sale_price" placeholder="Tankpa titik/koma (cth: 150000)" class="col-xs-10 col-sm-5"  onkeypress='return validateNumber(event)' required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="agent_price"> Harga Agen </label>

                    <div class="col-sm-9">
                        <input type="text" id="agent_price" name="agent_price" placeholder="Tankpa titik/koma (cth: 150000)" class="col-xs-10 col-sm-5"  onkeypress='return validateNumber(event)' required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="agent_price"> Harga Member </label>

                    <div class="col-sm-9">
                        <input type="text" id="agent2_price" name="agent2_price" placeholder="Tankpa titik/koma (cth: 150000)" class="col-xs-10 col-sm-5"  onkeypress='return validateNumber(event)' required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="start_dat"> Tanggal Harga Berlaku </label>

                    <div class="col-sm-9">
                        <input type="text" class="date-picker" required name="start_dat" id="start_dat" placeholder="Tanggal awal"></input>
                        &nbsp;s/d&nbsp;<input type="text" required class="date-picker" name="end_dat" id="end_dat" placeholder="Tanggal akhir"></input>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right">&nbsp;</label>
                    <div class="col-sm-9">
                        <input type="hidden" name="prodId" value="<?php echo $this->prodId;?>"/>
                        <input type="hidden" name="product_price_id" value="<?php echo $this->data['product_price_id'];?>"/>
                    
                        <button class="btn btn-success" type="button" id="btnTambah">
                            <i class="icon-ok bigger-110"></i>
                            Tambah Harga
                        </button>
                    </div>
                </div>
                    </div>
                <br>
                <table id="grid-table"></table>

                <div id="grid-pager"></div>
                <br>
                
                <script type="text/javascript">
                        var $path_base = "/";//this will be used in gritter alerts containing images
                </script>

        </form>
    </div>
</div>
<script>
    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

    var satrtDate = $('#start_dat').datepicker({
      format: 'yyyy-mm-dd'
    }).on('changeDate', function(ev) {
      //if (ev.date.valueOf() > endDate.date.valueOf()) {
        var newDate = new Date(ev.date)
        newDate.setDate(newDate.getDate() + 1);
        endDate.setValue(newDate);
      //}
      satrtDate.hide();
      $('#end_dat')[0].focus();
    }).data('datepicker');

    var endDate = $('#end_dat').datepicker({
      onRender: function(date) {
        return date.valueOf() <= satrtDate.date.valueOf() ? 'disabled' : '';
      }, format: 'yyyy-mm-dd'
    }).on('changeDate', function(ev) {
      endDate.hide();
    }).data('datepicker');

    function validateNumber(evt) {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
      }


    jQuery(function($) {

            jQuery('#grid-table').jqGrid({
                url:'<?php echo $this->url() ?>/../../jsondata/productpricelist?prodId=<?php echo $this->prodId;?>',
                datatype: "json",
                height: 150,
                colNames:['Harga Asal','Harga Jual','Harga Agen','Harga Member','Tanggal Harga Berlaku','Aksi'],
                colModel:[
                    {name:'HargaAsal',index:'basic_price', align:"right", formatter: 'number'},
                    {name:'HargaJual',index:'sale_price', align:"right", formatter: 'number'},
                    {name:'HargaAgen',index:'agent_price', align:"right", formatter: 'number'},
                    {name:'HargaMember',index:'agent2_price', align:"right", formatter: 'number'},
                    {name:'TglHarga',index:'start_dat', align:"right"},
                    {name:'Aksi',index:'aksi',align:"center"}
                ],
                rowNum:10,
                rowList:[10,20,30],
                pager: '#grid-pager',
                rownumbers: true,
                viewrecords: true,
                multiboxonly: true,
                caption:"Daftar Harga Produk",

                loadComplete : function() {
                        var table = this;
                        setTimeout(function(){
                                updateActionIcons(table);
                                updatePagerIcons(table);
                                enableTooltips(table);
                        }, 0);
                },

                autowidth: true
            });

            jQuery('#grid-table').jqGrid('navGrid','#grid-pager',{edit:false,add:false,del:false});


            //navButtons
            jQuery('#grid-table').jqGrid('navGrid','#grid-pager',
                    { 	//navbar options
                            search: true,
                            searchicon : 'ui-icon-search orange',
                            refresh: true,
                            refreshicon : 'ui-icon-refresh green'
                    }
            )




            //unlike navButtons icons, action icons in rows seem to be hard-coded
            //you can change them like this in here if you want
            function updateActionIcons(table) {

                    var replacement =
                    {
                            'ui-icon-search' : 'ui-icon-search orange',
                            'ui-icon-refresh' : 'ui-icon-refresh green'
                    };
                    $(table).find('.ui-pg-div span.ui-icon').each(function(){
                            var icon = $(this);
                            var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
                            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                    })

            }

            //replace icons with FontAwesome icons like above
            function updatePagerIcons(table) {
                    var replacement =
                    {
                            'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
                            'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
                            'ui-icon-seek-next' : 'icon-angle-right bigger-140',
                            'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
                    };
                    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
                            var icon = $(this);
                            var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                    })
            }

            function enableTooltips(table) {
                    $('.navtable .ui-pg-button').tooltip({container:'body'});
                    $(table).find('.ui-pg-div').tooltip({container:'body'});
            }

            //var selr = jQuery(grid_selector).jqGrid('getGridParam','selrow');

    });

    
    var idRow = 1;
    var totalHarga = 0;
    jQuery("#btnTambah").click( function(){

        var price1 = $('#basic_price').val();
        var price2 = $('#sale_price').val();
        var price3 = $('#agent_price').val();
        var price4 = $('#agent2_price').val();
        var date1 = $('#start_dat').val();
        var date2 = $('#end_dat').val();

        if (price1 == "" || price2 == "" || price3 == "" || price4 == "" || date1 == ""){
            bootbox.dialog({
                    message: "<span class='bigger-110'>Data tidak lengkap.</span>",
                    buttons:
                    {
                            "button" :
                             {
                                    "label" : "<i class='icon-ok'></i> Ok",
                                    "className" : "btn-sm btn-primary",
                                    //"callback": function() {
                                            //Example.show("great success");
                                    //}
                            }
                    }
            });
            
            return false;
        }

        jQuery("#grid-table").jqGrid('setGridParam',{
                    url:"<?php echo $this->url() ?>/../../jsondata/productpricelist?prodId=<?php echo $this->prodId;?>&price1="+price1+"&price2="+price2+"&price3="+price3+"&price4="+price4+"&date1="+date1+"&date2="+date2+"&act=add"
                }).trigger("reloadGrid");
                
    });


    function del(prodPriceId){

        bootbox.confirm("Hapus Harga?", function(result) {
                if(result) {
                        jQuery("#grid-table").jqGrid('setGridParam',{
                            url:"<?php echo $this->url() ?>/../../jsondata/productpricelist?prodId=<?php echo $this->prodId;?>&prodPriceId="+prodPriceId+"&act=del"
                        }).trigger("reloadGrid");
                }
        });

    }
    
</script>
<?php } ?>